<?php


namespace Modules\Pmcsignage\Http\Repositories;

use App\Services\KingDeeService;
use App\Services\UnitService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Modules\Inventory\Repository\InventoryOrderRepository;
use Modules\Purchasing\Models\Provisional;
use Modules\Purchasing\Models\Provisionalschedule;
use Illuminate\Support\Facades\DB;
use Catch\Exceptions\FailedException;
use Modules\Base\Service\BoxService;
use Modules\Purchasing\Models\Supplier;
use Modules\Purchasing\Models\Supplierschedule;
use Modules\Purchasing\Models\Purchaseorder;
use Modules\Purchasing\Models\Purchaseorderschedule;
use Modules\Warehouse\Models\Storagelocation;
use Modules\Purchasing\Models\Inspectionlist;
use Modules\Purchasing\Models\Inspectionlistcheck;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Modules\Intelligent\Http\Repositories\InspectionlistReget;
use Modules\Intelligent\Models\Otherstorage;
use Modules\Intelligent\Models\Otherstorageschedule;
use Modules\Intelligent\Models\Lotbinding;
use Modules\Takestock\Models\InventoryOrderDetailschedule;
use Modules\Inventory\Models\InventoryOrderDetail;
use Modules\Intelligent\Models\Otheroutboundfrom;
use Modules\Intelligent\Models\OtheroutboundfromSchedule;
use App\Enums\InvTransDocType;
use App\Services\InventoryService;
use App\Enums\InventoryOrderStatus;
use Modules\Production\Models\ProductionOrder;
use Modules\Production\Models\ProductionOrderDetail;
use Modules\Pmcsignage\Models\ProductionTheweekplan;


class TodayproductionReget
{

    public static function listtable($data = [])
    {
        
        return self::getBuildertable($data)->paginate($data['limit'] ?? 20);
    }


    public static function getBuildertable(array $data = []): Builder
    {

        $builder = ProductionOrder::query();
        if (isset($data['created_at'])) {
            $str = strtotime($data['created_at'].' 00:00');
            $end = strtotime($data['created_at'].' 23:59');
            $builder->where('created_at','>=' , $str);
            $builder->where('created_at','<=' , $end);
        }
        
        if (isset($data['back_status'])) {
            $builder->where('back_status', $data['back_status']);
        }
        
        if (isset($data['plantype'])) {
             
            if(!empty($data['plantype'])){
                
                $plantype = ProductionTheweekplan::select('plantype','production_order_id')->where('plantype',$data['plantype'])->get()->toArray();
                if(!empty($plantype)){
                    foreach($plantype as $k=>$v){
                        $production_order_id[] = $v['production_order_id'];
                    }
                    $production_order_id = array_unique($production_order_id);
               
                   $builder->whereIn('id', $production_order_id);
                }else {
                     $builder->where('id', '');
                }
            }
           
        }
        
        if (isset($data['fbillno'])) {
         
            $builder->where('fbillno', 'like', "%{$data['fbillno']}%");
        }
        
        if (isset($data['fmaterialid'])) {
           
             $builder->where('fmaterialid', 'like', "%{$data['fmaterialid']}%");
        }
        
        if (isset($data['fmaterialname'])) {
           $builder->where('fmaterialname', 'like', "%{$data['fmaterialname']}%");
        }
        
        if (isset($data['fworkshopid_name'])) {
             $builder->where('fworkshopid_name', 'like', "%{$data['fworkshopid_name']}%");
        }
        
        if (isset($data['fline'])) {
            $builder->where('fline', 'like', "%{$data['fline']}%");
        }
        
        

        $builder->orderBy('id', 'desc');
        return $builder;
    }


    public static function listtable_goodstock($data = [])
    {
     
        return self::getBuildertable_goodstock($data)->paginate($data['limit'] ?? 20);
    }


    public static function getBuildertable_goodstock(array $data = []): Builder
    {

        $builder = ProductionOrderDetail::query();

        if (isset($data['created_at'])) {
            $str = strtotime($data['created_at'].' 00:00');
            $end = strtotime($data['created_at'].' 23:59');
            $builder->where('created_at','>=' , $str);
            $builder->where('created_at','<=' , $end);
        }
        
        if (isset($data['plantype'])) {
            if(!empty($data['plantype'])){
                $plantype = ProductionTheweekplan::select('plantype','production_order_id')->where('plantype',$data['plantype'])->get()->toArray();
                if(!empty($plantype)){
                    foreach($plantype as $k=>$v){
                    $production_order_id[] = $v['production_order_id'];
                    }
                    $production_order_id = array_unique($production_order_id);
                    
                    
                    $builder->whereIn('production_order_id', $production_order_id);
                }else {
                     $builder->where('id', '');
                }
            }
        }

        if (isset($data['back_status'])) {

            $ProductionOrder_ID = ProductionOrder::select('id')->where('back_status',$data['back_status'])->get()->toArray();
            foreach($ProductionOrder_ID as $k=>$v){
                $id[] = $v['id'];
            }
            $builder->whereIn('production_order_id', $id);
        }
        
         if (isset($data['fbillno'])) {
            $fbillno = ProductionOrder::select('fbillno','id')->where('fbillno', 'like', "%{$data['fbillno']}%")->get()->toArray();
             $fmaterial_fbillno = [];
            foreach($fbillno as $k=>$v){
                $fmaterial_fbillno[] = $v['id'];
            }
            $fmaterial_fbillno = array_unique($fmaterial_fbillno);
            
            $builder->whereIn('production_order_id',$fmaterial_fbillno);
        }
        
        if (isset($data['fmaterialid1'])) {
            $builder->where('fmaterialid1', 'like', "%{$data['fmaterialid1']}%");
        }
        
        if (isset($data['fmaterialname1'])) {
            $builder->where('fmaterialname1', 'like', "%{$data['fmaterialname1']}%");
        }
        
        
        

        $builder->orderBy('id', 'desc');
        return $builder;
        
    }

   





}
